using System.Collections.Generic;
using System.Linq;

namespace Kiwi.Container.Lifetime
{
    public class CompositeLifetimePolicy : ILifetimePolicy
    {
        private readonly ILifetimePolicy[] _policies;

        public CompositeLifetimePolicy(IEnumerable<ILifetimePolicy> policies)
        {
            _policies = policies.ToArray();
        }

        #region ILifetimePolicy Members

        public object StartLife(object component)
        {
            foreach (var policy in _policies)
            {
                component = policy.StartLife(component);
            }
            return component;
        }

        public object EndLife(object component)
        {
            foreach (var policy in _policies.Reverse())
            {
                component = policy.EndLife(component);
            }
            return component;
        }

        #endregion
    }
}